import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-09-24
 * Time: 0:52
 */
public class Exercise {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);



        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        String strs = null;

        String str = "a,b,c,d,e";

        for (int i = 0; i < n; i++) {
            strs = in.next(String.valueOf(i));
        }

        byte[] strss= strs.getBytes();



        System.out.println(ret(str1,str2,strss,n));
    }

    public static int ret(String str1,String str2,byte[] strs,int n) {

        if(str1 == null || str2 == null){
            return -1;
        }

        int len = Integer.MAX_VALUE;
        int str1pos = -1;
        int str2pos = -1;
        for(int i = 0; i < n; i++){

            if(strs[i].equals(str1)){
                len = Math.min(len,len == -1 ? -1 : i-str2pos);
                str1pos = i;
            }


            if(strs[i].equals(str2)){
                len = Math.min(len,len == -1 ? -1 : i-str1pos);
                str2pos = i;
            }

        }

        return len == Integer.MAX_VALUE ? -1 : len;
    }
}
